package com.jx.medical.controller;

import com.jx.medical.pojo.AjaxInfo;
import com.jx.medical.pojo.Factory;
import com.jx.medical.pojo.Instorage;
import com.jx.medical.pojo.Medicine;
import com.jx.medical.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author jx
 * @date Created on 2020/12/4  10:24
 **/
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/insertStorage")
    public AjaxInfo insertStroage(@RequestBody Instorage instorage, HttpSession session) {
        System.out.println(instorage);
        AjaxInfo ajaxInfo = new AjaxInfo();
        Instorage in = new Instorage();
        if (session.getAttribute("userName") != null) {
            Medicine med = adminService.getMedicineBymedId(instorage.getMedId());
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = new Date();
            in.setMedId(instorage.getMedId());
            in.setBuyerId(instorage.getBuyerId());
            in.setInDate(time.format(date));
            in.setInStorageNum(instorage.getInStorageNum());
            if (med == null) {
                ajaxInfo.setCode(-1);
                ajaxInfo.setMsg("入库失败，该药品不存在！");
            } else {
                adminService.insertInstorage(in);
                adminService.updateMedStorage(instorage.getMedId(), instorage.getInStorageNum()+med.getMedStorage());
                ajaxInfo.setMsg("入库成功~");
                ajaxInfo.setCode(0);
            }

            return ajaxInfo;

        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;

    }

    @RequestMapping("/insertMedicine")
    public AjaxInfo insertMedicine(@RequestBody Medicine medicine, HttpSession session) {
        System.out.println(medicine);
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            Medicine med = adminService.getMedicineBymedId(medicine.getMedId());
            if (med == null) {
                adminService.insertMedicine(medicine);
                ajaxInfo.setMsg("插入信息成功~");
                ajaxInfo.setCode(0);
            } else {
                ajaxInfo.setMsg("该药品已存在~");
                ajaxInfo.setCode(-1);
            }
            return ajaxInfo;
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/queryMedicine")
    public AjaxInfo queryMedicine(@RequestBody Medicine medicine, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            Medicine med = adminService.getMedicineBymedId(medicine.getMedId());
            if (med == null) {
                ajaxInfo.setMsg("该药品不存在，请重新输入！");
                ajaxInfo.setCode(-1);
            } else {
                ajaxInfo.setMsg("查询成功！");
                ajaxInfo.setData(med);
                ajaxInfo.setCode(0);
                System.out.println(med);
            }
            return ajaxInfo;
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
            ajaxInfo.setCode(-2);
        }

        return ajaxInfo;
    }

    @RequestMapping("/updateMedicine")
    public AjaxInfo updateMedicine(@RequestBody Medicine medicine, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            adminService.updateMedicine(medicine);
            ajaxInfo.setMsg("更新成功！");
            ajaxInfo.setCode(0);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/deleteMedicine")
    public AjaxInfo deleteMedicine(@RequestBody Medicine medicine, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            adminService.deleteMedicine(medicine.getMedId());
            ajaxInfo.setMsg("删除成功！");
            ajaxInfo.setCode(0);
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }
    @RequestMapping("/insertFactory")
    public AjaxInfo insertFactory(@RequestBody Factory factory, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            Factory fId = adminService.getFactoryByfactoryId(factory.getFactoryId());
            if (fId == null) {
                adminService.insertFactory(factory);
                ajaxInfo.setMsg("插入信息成功~");
                ajaxInfo.setCode(0);
            } else {
                ajaxInfo.setMsg("该药厂已存在~");
                ajaxInfo.setCode(-1);
            }
            return ajaxInfo;
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }
    @RequestMapping("/queryFactory")
    public AjaxInfo queryFactory(@RequestBody Factory factory, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            Factory fId = adminService.getFactoryByfactoryId(factory.getFactoryId());
            if (fId == null) {
                ajaxInfo.setMsg("该药厂不存在，请重新输入！");
                ajaxInfo.setCode(-1);
            } else {
                ajaxInfo.setMsg("查询成功！");
                ajaxInfo.setData(fId);
                ajaxInfo.setCode(0);
                System.out.println(fId);
            }
            return ajaxInfo;
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
            ajaxInfo.setCode(-2);
        }
        return ajaxInfo;
    }
    @RequestMapping("/updateFactory")
    public AjaxInfo updateFactory(@RequestBody Factory factory, HttpSession session) {
        System.out.println(factory);
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            adminService.updateFactory(factory);
            ajaxInfo.setMsg("更新成功！");
            ajaxInfo.setCode(0);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/deleteFactory")
    public AjaxInfo deleteFactory(@RequestBody Factory factory, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            adminService.deleteFactory(factory.getFactoryId());
            ajaxInfo.setMsg("删除成功！");
            ajaxInfo.setCode(0);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }
}
